package mock_seed

import (
	"time"
	"wms/internal/model"
	"wms/pkg/database"
	"wms/pkg/logger"

	"go.uber.org/zap"
)

// InitLogisticsCompanies 初始化物流公司数据
func InitLogisticsCompanies() error {
	logger.Info("开始初始化物流公司数据...")
	db := database.GetDB()

	// 清空现有物流公司数据
	if err := db.Exec("DELETE FROM logistics_companies").Error; err != nil {
		logger.Error("清空物流公司数据失败", zap.Error(err))
		return err
	}

	// 物流公司列表
	companies := []model.LogisticsCompany{
		{
			Code:     "SF-COLD",
			Name:     "顺丰冷运",
			Contact:  "客服中心",
			Phone:    "95338",
			Email:    "coldchain@sf-express.com",
			Address:  "深圳市宝安区福永大道",
			Status:   1,
			Website:  "https://www.sf-express.com/cn/sc/express/cold-chain",
			APIKey:   "sfcold_api_12345678",
			TrackURL: "https://www.sf-express.com/cn/sc/dynamic_function/waybill",
			Coverage: "全国冷链配送网络",
			Level:    3,
			Remark:   "国内领先的冷链物流企业，可提供-25°C至25°C全温层冷链服务",
		},
		{
			Code:     "JD-COLD",
			Name:     "京东冷链",
			Contact:  "企业客服",
			Phone:    "950616",
			Email:    "jdcold@jd.com",
			Address:  "北京市通州区经济开发区",
			Status:   1,
			Website:  "https://www.jdwl.com/cold",
			APIKey:   "jdcold_api_23456789",
			TrackURL: "https://www.jdwl.com/order/search",
			Coverage: "全国主要城市",
			Level:    3,
			Remark:   "电商冷链龙头，拥有自建冷库和冷藏车队，全程温控监测",
		},
		{
			Code:     "ZTO-FRESH",
			Name:     "中通冷链",
			Contact:  "冷链事业部",
			Phone:    "95311",
			Email:    "coldservice@zto.com",
			Address:  "上海市青浦区华新镇华腾路",
			Status:   1,
			Website:  "https://www.zto.com/express/coldchain",
			APIKey:   "ztocold_api_34567890",
			TrackURL: "https://www.zto.com/express/expressCheck.html",
			Coverage: "华东、华北、华南重点城市",
			Level:    2,
			Remark:   "提供0°C至15°C冷藏和-18°C冷冻服务，主要面向生鲜电商",
		},
		{
			Code:     "HAVI",
			Name:     "海慧供应链",
			Contact:  "商务部",
			Phone:    "400-820-2033",
			Email:    "info@havi.com.cn",
			Address:  "上海市徐汇区虹桥路",
			Status:   1,
			Website:  "https://www.havi.com/cn",
			APIKey:   "havi_api_45678901",
			TrackURL: "https://www.havi.com/cn/tracking",
			Coverage: "全国餐饮冷链配送",
			Level:    3,
			Remark:   "专注于餐饮业冷链物流，为知名快餐品牌提供全冷链配送服务",
		},
		{
			Code:     "PFS",
			Name:     "普菲斯冷链物流",
			Contact:  "张经理",
			Phone:    "400-888-6677",
			Email:    "service@pfs-logistics.com",
			Address:  "广州市黄埔区经济开发区",
			Status:   1,
			Website:  "https://www.pfs-logistics.com",
			APIKey:   "pfs_api_56789012",
			TrackURL: "https://www.pfs-logistics.com/track",
			Coverage: "华南地区冷链专线",
			Level:    2,
			Remark:   "华南地区知名冷链物流企业，专注冷冻食品运输",
		},
		{
			Code:     "SNOWMAN",
			Name:     "雪人冷链",
			Contact:  "李总监",
			Phone:    "400-111-9876",
			Email:    "sales@snowman-logistics.com",
			Address:  "北京市大兴区经济开发区",
			Status:   1,
			Website:  "https://www.snowman-logistics.com.cn",
			APIKey:   "snowman_api_67890123",
			TrackURL: "https://www.snowman-logistics.com.cn/tracking",
			Coverage: "华北、东北冷链物流",
			Level:    2,
			Remark:   "专注于冰鲜冷冻食品运输，在极寒地区有丰富经验",
		},
		{
			Code:     "XC-COLD",
			Name:     "新创冷链物流有限公司",
			Contact:  "王经理",
			Phone:    "400-123-4567",
			Email:    "contact@xc-cold.com",
			Address:  "上海市松江区新桥镇",
			Status:   1,
			Website:  "https://www.xc-cold.com",
			APIKey:   "xccold_api_78901234",
			TrackURL: "https://www.xc-cold.com/track",
			Coverage: "华东、华中地区",
			Level:    2,
			Remark:   "区域性冷链物流服务商，专注城市配送",
		},
		{
			Code:     "FM-COLD",
			Name:     "丰鲜冷链物流股份有限公司",
			Contact:  "赵总",
			Phone:    "400-789-1234",
			Email:    "info@fengxian.com",
			Address:  "山东省青岛市胶州湾冷链物流基地",
			Status:   1,
			Website:  "https://www.fengxian.com",
			APIKey:   "fmcold_api_89012345",
			TrackURL: "https://www.fengxian.com/trace",
			Coverage: "山东、江苏、安徽地区",
			Level:    2,
			Remark:   "山东地区冷链物流骨干企业，专注海产品冷链运输",
		},
		{
			Code:     "ZHENGWEI",
			Name:     "正威国际冷链物流集团",
			Contact:  "商务部",
			Phone:    "400-666-8888",
			Email:    "business@zhengwei.com.cn",
			Address:  "深圳市南山区科技园北区",
			Status:   1,
			Website:  "https://www.zhengwei.com.cn",
			APIKey:   "zhengwei_api_90123456",
			TrackURL: "https://www.zhengwei.com.cn/tracking",
			Coverage: "全国及东南亚国际冷链",
			Level:    3,
			Remark:   "大型冷链物流集团，同时提供国际冷链物流服务",
		},
		{
			Code:     "CJ-COLD",
			Name:     "长久冷链物流有限公司",
			Contact:  "钱经理",
			Phone:    "400-100-2022",
			Email:    "service@cj-cold.com",
			Address:  "天津市西青区微电子工业区",
			Status:   1,
			Website:  "https://www.cj-cold.com",
			APIKey:   "cjcold_api_01234567",
			TrackURL: "https://www.cj-cold.com/track",
			Coverage: "华北、西北地区",
			Level:    2,
			Remark:   "北方地区知名冷链物流企业，严格温控管理",
		},
	}

	// 添加物流公司
	now := time.Now()
	for i, company := range companies {
		company.CreatedAt = now
		company.UpdatedAt = now
		company.ID = uint(i + 1) // 确保ID从1开始连续

		// 直接创建物流公司，不检查是否存在
		if err := db.Create(&company).Error; err != nil {
			logger.Error("创建物流公司失败", zap.String("name", company.Name), zap.Error(err))
			return err
		}
		logger.Info("创建物流公司成功", zap.String("name", company.Name))
	}

	logger.Info("物流公司数据初始化成功")
	return nil
}
